﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLL.Base;
using CommandService;
using Entity;
using Entity.SysEntity.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Entity.SysEntity;
using Entity.ViewEntity.sys;
using Entity.DbEntity;
using BLL.custom;
using Entity.ViewEntity;
namespace BLL.Basis
{
    /// <summary>
    /// 基础数据通用业务逻辑处理方法
    /// </summary>
    public class BaseDataBLL : PubMethod
    {

        Commands cmd;
        LoginInfo userLogin;
        List<OperateModel> operList;
        OperateModel oper;
        ReturnResult rsl;
        public BaseDataBLL()
        {
            //数据交互
            cmd = new Commands();
            //当前登录人信息
            userLogin = new LoginInfo();
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public ReturnResult getUserList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" status=1");
                if (userLogin.UserInfo().isAdmin != 1)
                {//非管理员权限
                    sb.Append(" and createUser='" + userLogin.UserInfo().userCode + "'");
                }
                sb = CreateSelectCondition(sb, condition);
                var list = cmd.PageList<ViewUserInfo>("V_UserInfo", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }


        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public ReturnResult getDeptList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" status='Y'");
                sb = CreateSelectCondition(sb, condition);
                var list = cmd.PageList<DbDeptInfo>("sys_DepartMent_Info", pageIndex, pageSize, sb.ToString(), "id ", ref pageCount);

                foreach (var s in list)
                {
                    if (s.isSync.ToString() == "Y")
                    {
                        s.isSync = "是";
                    }
                    else
                    {
                        s.isSync = "否";
                    }

                    if (s.status.ToString() == "Y")
                    {
                        s.status = "是";
                    }
                    else
                    {
                        s.status = "否";
                    }
                }

                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 获取全部有效的用户数据（U8职员档案）
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult AllPeoPleList(string condition)
        {
            rsl = new ReturnResult();
            CuDbHelper cuDb = new CuDbHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select p.* ,d.cDepName from Person p left join Department d on d.cDepCode = p.cDepCode where 1=1 ");
                if (!string.IsNullOrWhiteSpace(condition))
                {
                    var json = JsonConvert.DeserializeObject<JObject>(condition);
                    foreach (JProperty obj in json.Properties())
                    {
                        if (!string.IsNullOrWhiteSpace(obj.Value.ToString()))
                        {
                            if (!string.IsNullOrWhiteSpace(sb.ToString()))
                            {
                                sb.Append(" and ");
                            }
                            sb.Append(obj.Name + " like '%" + obj.Value.ToString() + "%' ");
                        }
                    }
                }
                var table = cuDb.getData(sb.ToString(), userLogin.UserInfo().baseCode);
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 临时岗位数据信息
        /// </summary>
        /// <param name="tempCode"></param>
        /// <returns></returns>
        public ReturnResult TempPositionList(string tempCode)
        {
            rsl = new ReturnResult();
            try
            {
                var table = (DataTable)Tools.Cache.GetCache(tempCode);
                if (table != null && table.Rows.Count >0)
                {
                    rsl.Data = cmd.TableToDictionary(table);
                    rsl.Result = true;
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 更新其他绩效数据信息(新建/修改)
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult UpdateWageStandard(string jsonStr)
        {
            ReturnResult rsl = new ReturnResult();
            try
            {
                var standard = JsonConvert.DeserializeObject<DbOtherStandard>(jsonStr);
                string sql = "select * from fun_OtherPrice_standard where sCode='" + standard.sCode + "'";
                var table = cmd.GetDataTable(sql);

                OperateModel oper = new OperateModel();
                List<OperateModel> list = new List<OperateModel>();
                if (standard.id != null && standard.id > 0)
                {
                    #region 修改数据
                    foreach (DataRow r in table.Rows)
                    {
                        if (r["id"].ToString() != standard.id.ToString().Trim())
                        {
                            rsl.Message = string.Format("该编码已经存在：{0}", standard.sCode);
                            return rsl;
                        }
                    }
                    #endregion
                    oper.Data = standard;
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";
                    list.Add(oper);
                }
                else
                {
                    #region 添加数据
                    if (table.Rows.Count > 0)
                    {
                        rsl.Message = string.Format("该编号已存在：{0}", standard.sCode);
                        return rsl;
                    }
                    standard.createtime = getNow();
                    standard.createuser = userLogin.UserInfo().userCode;
                    standard.delTime = DateTime.Now.ToString();
                    standard.status = "Y";
                    standard.isDel = 0;
                    oper.Data = standard;
                    oper.Types = OperateModel.OperateType.Add;
                    list.Add(oper);
                    #endregion
                }
                rsl = cmd.MoreUpdate(list);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 根据id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnResult getStandardById(string id)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_OtherPrice_standard where ");
                sb.Append("id=" + id);
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 根据id删除基础标准
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnResult DelStandardById(string id)
        {
            rsl = new ReturnResult();
            try
            {
                DbOtherStandard standard = new DbOtherStandard();
                standard.id = int.Parse(id);
                standard.isDel = 1;
                standard.delTime = getNow();
                standard.delUser = userLogin.UserInfo().userCode;
                OperateModel oper = new OperateModel();
                oper.Data = standard;
                oper.Conditions = "id";
                oper.Types = OperateModel.OperateType.Edit;
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

     
        /// <summary>
        ///通用数据显示方法
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult getCommonData(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(pageIndex))
                {
                    pageIndex = "1";
                }
                if (string.IsNullOrWhiteSpace(pageSize))
                {
                    pageSize = Tools.ConfigReader.Reader["PageSize"];
                }
                StringBuilder sb = new StringBuilder();
                sb.Append("isdel=0 and specFile='" + userLogin.UserInfo().baseCode + "' ");
                sb = CreateSelectCondition(sb, condition);
                rsl.Data = cmd.PageList<DbCommonData>("fun_CommonData_Info", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                rsl.Result = true;


            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }

            return rsl;

        }
        /// <summary>
        /// 获取通用数据集合(不分页)
        /// </summary>
        /// <param name="condition">检索条件</param>
        /// <returns></returns>
        public ReturnResult getCommonDataInfo( string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select top 200 * from fun_CommonData_Info where isdel=0 and ");
                sb.Append("specFile='" + userLogin.UserInfo().baseCode + "' ");
                sb = CreateSelectCondition(sb, condition);
                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }

            return rsl;

        }
        /// <summary>
        /// 客户通用数据获取方法
        /// </summary>
        /// <param name="type">类型编码</param>
        /// <returns></returns>
        public ReturnResult GetCommonData(string type)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_CommonData_Info where");
                sb.Append(" isdel=0 and ctype='" + type + "' and status='Y'");
                var table = cmd.GetDataTable(sb.ToString());
                if (table.Rows.Count > 0)
                {
                    rsl.Data = cmd.TableToDictionary(table);
                    rsl.Result = true;
                }
                else
                {
                    rsl.Message = "没有数据";
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
       
        /// <summary>
        /// 通用数据列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult getCommonDataList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                //var arr = condition.Split(',').ToList();
                var json = JsonConvert.DeserializeObject<JObject>(condition);
                StringBuilder sb = new StringBuilder();
                sb.Append("  isdel=0 ");
                sb.Append("  and cType='" + json["cType"].ToString() + "'");
                if (json.Property("parentCode") != null && !string.IsNullOrWhiteSpace(json["parentCode"].ToString()))
                {
                    sb.Append(" and parentCode='" + json["parentCode"].ToString() + "'");
                }
                var list = cmd.PageList<DbCommonData>("fun_CommonData_Info", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                rsl.Data = list;
                rsl.Result = true;    
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

      
        /// <summary>
        /// 人员岗位数据列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult getUserPositionList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" isdel=0 and baseCode='"+ userLogin.UserInfo().baseCode +"'");
                sb = CreateSelectCondition(sb, condition);
                var list = cmd.PageList("V_Users_GangWei", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 获取人员岗位信息
        /// </summary>
        /// <param name="gxCode">工序编码</param>
        /// <param name="machineCode">机台编码</param>
        /// <returns></returns>
        public ReturnResult getUserPositionByGX(string condition)
        {
            rsl = new ReturnResult();
            try
            {
                var json = JsonConvert.DeserializeObject<JObject>(condition);
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from V_Users_Group where groupCode='" + json["groupCode"].ToString() + "' ");
                sb.Append(" and baseCode='" + userLogin.UserInfo().baseCode + "'");
                var list = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
      /// <summary>
      /// 通用数据类型
      /// </summary>
      /// <param name="type">数据类型编码</param>
      /// <returns></returns>
        public ReturnResult getCommonDataType(string type)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select distinct ctype,cmemo from fun_CommonData_Info where (isdel=0");
                sb.Append(" and status='Y' and cvalues='2'");
                if (!string.IsNullOrWhiteSpace(type))
                {
                    sb.Append(" and parentCode='" + type + "'");
                }
                sb.Append(") or cType='scgxlx' ");
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 通用数据顶级菜单
        /// </summary>
        /// <param name="type">数据类型编码</param>
        /// <returns></returns>
        public ReturnResult getCommonDataTop()
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select cCode,cName from fun_CommonData_Info where isdel=0");
                sb.Append(" and status='Y' and parentCode='0'");
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        // 通用数据 获取父节点编码
        public ReturnResult getParentCodes()
        {
            rsl = new ReturnResult();
            try
            {
                var sql = "select * from fun_CommonData_Info where parentCode = '0';";
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sql));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 根据id获取通用数据
        /// </summary>
        /// <param name="dataId">数据id</param>
        /// <returns></returns>
        public ReturnResult getCommonDataById(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_CommonData_Info where ");
                sb.Append("id=" + dataId);
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 通用数据更新方法
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public ReturnResult UpdateCommonData(string datas)
        {
            rsl = new ReturnResult();
            try
            {
                oper = new OperateModel();
                var data = JsonConvert.DeserializeObject<DbCommonData>(datas);
                if (data.id == null || data.id == 0)
                {
                    string sql = "";
                    if (data.cType == "")
                    {
                        data.cValues = "2";
                        sql = "select * from fun_CommonData_Info where cType='" + data.cCode + "' and cValues='2' and isDel=0 and status='Y' ";
                        var table = cmd.GetDataTable(sql);
                        if (table.Rows.Count > 0)
                        {
                            rsl.Message = "编码存在重复";
                            return rsl;
                        }
                        var db = new DbCommonData();
                        db.cType = data.cCode;
                        db.cMemo = data.cName;
                        db.cValues = "2";
                        db.isDel = 0;
                        db.status = "Y";
                        db.parentCode = data.parentCode;
                        rsl = cmd.Add(db);
                    }
                    else
                    {
                        var json = JsonConvert.DeserializeObject<JObject>(datas);
                        sql = "select cCode from fun_CommonData_Info where cCode='" + data.cCode + "' and isDel=0 and status='Y' ";
                       var  table = cmd.GetDataTable(sql);
                        if (table.Rows.Count > 0)
                        {
                            rsl.Message = "编码存在重复";
                            return rsl;
                        }
                        data.parentCode = "3";
                        data.createtime = getNow();
                        data.createuser = userLogin.UserInfo().userCode;
                        if (data.cType == "scgxlx")
                        {
                            data.orderNum = json["parentCode"].ToString();
                        }
                        data.isDel = 0;
                        data.status = "Y";
                        rsl = cmd.Add(data);
                    }
                
                }
                else
                {
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";
                    if (string.IsNullOrWhiteSpace(data.specFile))
                    {
                        data.specFile = "-";
                    }
                    if (string.IsNullOrWhiteSpace(data.cMemo))
                    {
                        data.cMemo = "-";
                    }
                    oper.Data = data;
                    rsl = cmd.Edit(oper);
                }

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 编辑通用数据名称
        /// </summary>
        /// <param name="id"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public ReturnResult EditCommonNames(string id, string values)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(values))
                {
                    rsl.Message = "修改内容不允许为空";
                    return rsl;
                }
                oper = new OperateModel();
                DbCommonData data = new DbCommonData();
                data.id = int.Parse(id);
                data.cName = values;
                oper.Data = data;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 删除共用数据方法
        /// </summary>
        /// <param name="dataId">数据id</param>
        /// <returns></returns>
        public ReturnResult DelCommonData(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                oper = new OperateModel();
                DbCommonData data = new DbCommonData();
                data.id = int.Parse(dataId);
                data.isDel = 1;
                data.delDate = getNow();
                data.delUser = userLogin.UserInfo().userCode;
                oper.Data = data;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 更新存货大类
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult UpdateInventoryClass(string jsonStr)
        {
            rsl = new ReturnResult();
            try
            {
                var iClass = JsonConvert.DeserializeObject<DbInventoryClass>(jsonStr);

                if (string.IsNullOrWhiteSpace(iClass.parentCode))
                {
                    rsl.Message = "上级编码为必填项！";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(iClass.cCode))
                {
                    rsl.Message = "类别编码为必填项！";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(iClass.cName))
                {
                    rsl.Message = "菜单名称不能为空！";
                    return rsl;
                }

                string sql = "select * from V_InvenToryClass_info ";
                var invenClassTable = cmd.GetDataTable(sql);

                OperateModel oper = new OperateModel();
                List<OperateModel> opersList = new List<OperateModel>();

                var cCodedr = invenClassTable.Select("cCode='" + iClass.cCode + "'");
                var pCodedr = invenClassTable.Select("cCode='" + iClass.parentCode + "'");

                if (cCodedr.Length > 0)
                {
                    if (cCodedr[0]["id"].ToString() != iClass.id.ToString().Trim())
                    {
                        rsl.Message = string.Format("类别编码已存在：{0}", iClass.cCode);
                        return rsl;
                    }
                }
                if (pCodedr.Length < 1)
                {
                    rsl.Message = string.Format("上级编码不存在：{0}", iClass.parentCode);
                    return rsl;
                }

                if (iClass.id != null && iClass.id > 0)
                {
                    #region 修改数据


                    oper.Data = iClass;
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";
                    opersList.Add(oper);
                    #endregion
                }
                else
                {
                    #region 添加数据
                    iClass.isDel = 0;
                    //iClass.status = "open";
                    iClass.isSync = "N";
                    iClass.baseCode = userLogin.UserInfo().baseCode;
                    iClass.createuser = userLogin.UserInfo().userCode;
                    iClass.createtime = DateTime.Now.ToString();

                    oper.Data = iClass;
                    oper.Types = OperateModel.OperateType.Add;
                    opersList.Add(oper);
                    #endregion
                }

                rsl = cmd.MoreUpdate(opersList);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;

        }

        /// <summary>
        /// 通过id获取存货大类信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult getInventoryClassById(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from V_InvenToryClass_info where ");
                if (!string.IsNullOrWhiteSpace(dataId))
                {
                    sb.Append("id=" + dataId);
                }
                else
                {
                    sb.Append("1>2");
                }
                var icTable = cmd.GetDataTable(sb.ToString());
                var list = cmd.TableToDictionary(icTable);
                //var tempList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(JsonConvert.SerializeObject(list));
                //if (tempList.Count > 0)
                //{
                //    foreach (var item in tempList)
                //    {
                //        if (item["status"].ToString().Equals("open"))
                //        {
                //            item["statusName"] = "启用";
                //        }
                //        else
                //        {
                //            item["statusName"] = "停用";
                //        }
                //    }
                //}
                //rsl.Data = tempList; 
                rsl.Data = list;
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;
        }

        /// <summary>
        /// 删除存货大类信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelInventoryClass(int dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbInventoryClass ic = new DbInventoryClass();

                ic.id = dataId;
                ic.isDel = 1;
                ic.delDate = getNowDate();
                ic.delUser = userLogin.UserInfo().userCode;

                oper = new OperateModel();
                oper.Data = ic;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";

                rsl = cmd.Edit(oper);
            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;
        }

        /// <summary>
        /// 更新仓库信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult UpdateWareHouseInfo(string jsonStr)
        {
            rsl = new ReturnResult();
            try
            {
                var wHouse = JsonConvert.DeserializeObject<DbWareHouse>(jsonStr);

                if (string.IsNullOrWhiteSpace(wHouse.houseCode))
                {
                    rsl.Message = "仓库编码为必填项！";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(wHouse.houseName))
                {
                    rsl.Message = "仓库名称不能为空！";
                    return rsl;
                }

                string sql = "select * from fun_WareHouse_Info where houseCode='" + wHouse.houseCode + "'";
                var whTable = cmd.GetDataTable(sql);

                oper = new OperateModel();
                List<OperateModel> opersList = new List<OperateModel>();

                if (wHouse.id != null && wHouse.id > 0)
                {
                    #region 修改数据
                    foreach (DataRow dr in whTable.Rows)
                    {
                        if (dr["id"].ToString() != wHouse.id.ToString().Trim())
                        {
                            rsl.Message = string.Format("仓库编码重复：{0}", wHouse.houseCode);
                            return rsl;
                        }
                    }
                    oper.Data = wHouse;
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";

                    opersList.Add(oper);
                    #endregion
                }
                else
                {
                    #region 添加数据
                    if (whTable.Rows.Count > 0)
                    {
                        rsl.Message = string.Format("仓库编码重复：{0}", wHouse.houseCode);
                        return rsl;
                    }
                    wHouse.isDel = 0;
                    //wHouse.status = "open";
                    wHouse.isSync = "N";
                    wHouse.baseCode = userLogin.UserInfo().baseCode;
                    wHouse.createuser = userLogin.UserInfo().userCode;
                    wHouse.createtime = DateTime.Now.ToString();

                    oper.Data = wHouse;
                    oper.Types = OperateModel.OperateType.Add;
                    opersList.Add(oper);
                    #endregion
                }

                rsl = cmd.MoreUpdate(opersList);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }
            return rsl;

        }

        /// <summary>
        /// 获取仓库信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getWareHouseInfoById(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_WareHouse_Info where ");

                if (!string.IsNullOrWhiteSpace(dataId))
                {
                    sb.Append("id=" + dataId);
                }
                else
                {
                    sb.Append("1>2");
                }

                var whTable = cmd.GetDataTable(sb.ToString());
                var list = cmd.TableToDictionary(whTable);
                rsl.Data = list; 
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }

        /// <summary>
        /// 获取仓库信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getWareHouseList(string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select houseCode code,houseName name from fun_WareHouse_Info where ");
                sb.Append("baseCode='" + userLogin.UserInfo().baseCode + "' and isDel=0 and status='open' ");
                var json = JsonConvert.DeserializeObject<JObject>(condition);
                foreach (JProperty obj in json.Properties())
                {
                    if (!string.IsNullOrWhiteSpace(obj.Value.ToString()))
                    {
                        if (!string.IsNullOrWhiteSpace(sb.ToString()))
                        {
                            sb.Append(" and ");
                        }
                        if(obj.Name.ToString().Equals("code"))
                            sb.Append("houseCode like '%" + obj.Value.ToString() + "%' ");
                        else if (obj.Name.ToString().Equals("name"))
                            sb.Append("houseName like '%" + obj.Value.ToString() + "%' ");
                    }
                }

                var whTable = cmd.GetDataTable(sb.ToString());
                var list = cmd.TableToDictionary(whTable);
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }
        
        /// <summary>
        /// 获取货位信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getPostionList(string condition,string typesCode)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select cCode code,cName name from fun_CommonData_Info where cType='WP' and status='Y' and isDel=0 ");
                sb.Append("and specFile='"+ userLogin.UserInfo().baseCode+ "' and parentCode='"+ typesCode + "'");
                var json = JsonConvert.DeserializeObject<JObject>(condition);
                foreach (JProperty obj in json.Properties())
                {
                    if (!string.IsNullOrWhiteSpace(obj.Value.ToString()))
                    {
                        if (!string.IsNullOrWhiteSpace(sb.ToString()))
                        {
                            sb.Append(" and ");
                        }
                        if (obj.Name.ToString().Equals("code"))
                            sb.Append("cCode like '%" + obj.Value.ToString() + "%' ");
                        else if (obj.Name.ToString().Equals("name"))
                            sb.Append("cName like '%" + obj.Value.ToString() + "%' ");
                    }
                }

                var psTable = cmd.GetDataTable(sb.ToString());
                var list = cmd.TableToDictionary(psTable);
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }

        /// <summary>
        /// 删除仓库信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelWareHouseInfo(int dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbWareHouse wh = new DbWareHouse();

                wh.id = dataId;
                wh.isDel = 1;
                wh.delTime = getNow();
                wh.delUser = userLogin.UserInfo().userCode;

                oper = new OperateModel();
                oper.Data = wh;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";

                rsl = cmd.Edit(oper);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 更新货位信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult UpdateWarePosition(string jsonStr)
        {
            rsl = new ReturnResult();
            try
            {
                var wPosition = JsonConvert.DeserializeObject<DbCommonData>(jsonStr);

                if (string.IsNullOrWhiteSpace(wPosition.cCode))
                {
                    rsl.Message = "货位编码为必填项！";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(wPosition.cName))
                {
                    rsl.Message = "货位名称不能为空！";
                    return rsl;
                }

                string sql = "select * from fun_CommonData_Info where cCode='" + wPosition.cCode + "'";
                var wpTable = cmd.GetDataTable(sql);

                oper = new OperateModel();
                List<OperateModel> opersList = new List<OperateModel>();

                if (wPosition.id != null && wPosition.id > 0)
                {
                    #region 修改数据
                    foreach (DataRow dr in wpTable.Rows)
                    {
                        if (dr["id"].ToString() != wPosition.id.ToString().Trim())
                        {
                            rsl.Message = string.Format("货位编码重复：{0}", wPosition.cCode);
                            return rsl;
                        }

                    }
                    oper.Data = wPosition;
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";

                    opersList.Add(oper);
                    #endregion
                }
                else
                {
                    #region 添加数据
                    if (wpTable.Rows.Count > 0)
                    {
                        rsl.Message = string.Format("货位编码重复：{0}", wPosition.cCode);
                        return rsl;
                    }
                    wPosition.isDel = 0;
                    //wPosition.status = "Y";
                    wPosition.cType = "WP";
                    wPosition.cMemo = "仓库货位";
                    wPosition.specFile = userLogin.UserInfo().baseCode;
                    wPosition.createuser = userLogin.UserInfo().userCode;
                    wPosition.createtime = DateTime.Now.ToString();

                    oper.Data = wPosition;
                    oper.Types = OperateModel.OperateType.Add;
                    opersList.Add(oper);

                    #endregion
                }

                rsl = cmd.MoreUpdate(opersList);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;

        }

        /// <summary>
        /// 获取货位信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getWarePositionById(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_CommonData_Info where ");

                if (!string.IsNullOrWhiteSpace(dataId))
                {
                    sb.Append("id=" + dataId);
                }
                else
                {
                    sb.Append("1>2");
                }

                var table = cmd.GetDataTable(sb.ToString());
                var list  = cmd.TableToDictionary(table);
                var tempList = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(JsonConvert.SerializeObject(list));
                if (tempList.Count > 0)
                {
                    foreach (var item in tempList)
                    {
                        if (item["status"].ToString().Equals("Y"))
                        {
                            item["statusName"] = "启用";
                        }
                        else
                        {
                            item["statusName"] = "停用";
                        }
                    }
                }
                rsl.Data = tempList;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }

        /// <summary>
        /// 删除货位信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelWarePosition(int dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbCommonData wp = new DbCommonData();

                wp.id = dataId;
                wp.isDel = 1;
                wp.delDate = DateTime.Now.ToString();
                wp.delUser = userLogin.UserInfo().userCode;

                oper = new OperateModel();
                oper.Data = wp;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";

                rsl = cmd.Edit(oper);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;
        }

        /// <summary>
        /// 更新存货档案
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult UpdateInventoryInfo(string jsonStr)
        {
            rsl = new ReturnResult();
            try
            {
                var iInfo = JsonConvert.DeserializeObject<DbInventory>(jsonStr);
                var barLength = int.Parse(Tools.ConfigReader.Reader["CodeLength"]);

                //if (string.IsNullOrWhiteSpace(iInfo.proCode))
                //{
                //    rsl.Message = "存货编码为必填项！";
                //    return rsl;
                //}
                if (string.IsNullOrWhiteSpace(iInfo.proName))
                {
                    rsl.Message = "存货名称不能为空！";
                    return rsl;
                }

                string invSql = "select count(*) as num from fun_Inventory_Info where typeCode='" + iInfo.typeCode + "'";
                string invcSql = "select * from fun_InvenClass_Info where parentCode='" + iInfo.typeCode + "'";

                var invenTable = cmd.GetDataTable(invSql);
                var invClassTable = cmd.GetDataTable(invcSql);

                OperateModel oper = new OperateModel();
                List<OperateModel> opersList = new List<OperateModel>();
                //var dr = invenTable.Select("proCode='" + iInfo.proCode + "'");
                //if (dr.Length > 0)
                //{
                //    if (dr[0]["id"].ToString() != iInfo.id.ToString().Trim())
                //    {
                //        rsl.Message = string.Format("存货编码已存在：{0}", iInfo.proCode);
                //        return rsl;
                //    }
                //}

                if (iInfo.id != null && iInfo.id > 0)
                {
                    #region 修改信息
                    oper.Data = iInfo;
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";

                    opersList.Add(oper);
                    #endregion
                }
                else
                {
                    #region 添加信息
                    if (invClassTable.Rows.Count > 0)
                    {
                        rsl.Message = ("该存货大类存在子类，请选择子类！");
                        return rsl;
                    }

                    var num = cmd.TableToDictionary(invenTable);
                    var newNum = "";
                    if (int.Parse(num[0]["num"]) > 0)
                    {
                        newNum = Convert.ToString(int.Parse(num[0]["num"]) + 1);
                        for (int i = 0; i < barLength - (int.Parse(num[0]["num"]) + 1).ToString().Length; i++)
                        {
                            newNum = "0" + newNum;
                        }
                    }
                    else
                    {
                        newNum = "1";
                        for (int i = 0; i < barLength - 1; i++)
                        {
                            newNum = "0" + newNum;
                        }
                    }
                   
                    
                    iInfo.proCode = iInfo.typeCode + newNum;
                    iInfo.isDel = 0;
                    iInfo.width = "0";
                    iInfo.isSync = "N";
                    iInfo.baseCode = userLogin.UserInfo().baseCode;
                    iInfo.createuser = userLogin.UserInfo().userCode;
                    iInfo.createtime = DateTime.Now.ToString();

                    oper.Data = iInfo;
                    oper.Types = OperateModel.OperateType.Add;
                    opersList.Add(oper);

                    #endregion
                }

                rsl = cmd.MoreUpdate(opersList);
            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }
            return rsl;

        }

        /// <summary>
        /// 获取存货档案信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getInventoryInfoById(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_Inventory_Info where ");

                if (!string.IsNullOrWhiteSpace(dataId))
                {
                    sb.Append("id=" + dataId);
                }
                else
                {
                    sb.Append("1>2");
                }

                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }

        /// <summary>
        /// 删除存货档案信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelInventoryInfo(int dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbInventory wp = new DbInventory();

                wp.id = dataId;
                wp.isDel = 1;
                wp.delDate = DateTime.Now.ToString();
                wp.delUser = userLogin.UserInfo().userCode;

                oper = new OperateModel();
                oper.Data = wp;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";

                rsl = cmd.Edit(oper);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;
        }

        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult UpdateCustomerInfo(string jsonStr)
        {
            rsl = new ReturnResult();

            try
            {
                var cInfo = JsonConvert.DeserializeObject<DbCustomer>(jsonStr);

                if (string.IsNullOrWhiteSpace(cInfo.cuCode))
                {
                    rsl.Message = "编码为必填项！";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(cInfo.cuName))
                {
                    rsl.Message = "名称不能为空！";
                    return rsl;
                }

                string sql = "select * from fun_Customs_info where dataType='"+ cInfo.dataType + "' and cuCode='" + cInfo.cuCode + "'";

                var cuTable = cmd.GetDataTable(sql);

                OperateModel oper = new OperateModel();
                List<OperateModel> opersList = new List<OperateModel>();

                if (cInfo.id != null && cInfo.id > 0)
                {
                    foreach (DataRow dr in cuTable.Rows)
                    {
                        if (dr["id"].ToString() != cInfo.id.ToString().Trim())
                        {
                            rsl.Message = string.Format("编码重复：{0}", cInfo.cuCode);
                            return rsl;
                        }

                    }

                    oper.Data = cInfo;
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";
                    opersList.Add(oper);
                }
                else
                {
                    if (cuTable.Rows.Count > 0)
                    {
                        rsl.Message = string.Format("编码重复：{0}", cInfo.cuCode);
                        return rsl;
                    }
                    cInfo.baseCode = userLogin.UserInfo().baseCode;
                    cInfo.isDel = 0;
                    cInfo.status = "Y";
                    cInfo.isSync = "N";
                    cInfo.createuser = userLogin.UserInfo().userCode;
                    cInfo.createtime = DateTime.Now.ToString();

                    oper.Data = cInfo;
                    oper.Types = OperateModel.OperateType.Add;
                    opersList.Add(oper);
                }


                //！执行语句！
                rsl = cmd.MoreUpdate(opersList);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;

        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getCustomerInfoById(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_CommonData_Info where ");

                if (!string.IsNullOrWhiteSpace(dataId))
                {
                    sb.Append("id=" + dataId);
                }
                else
                {
                    sb.Append("1>2");
                }

                var table = cmd.GetDataTable(sb.ToString());
                var list = cmd.TableToDictionary(table);
                
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelCustomerInfo(int dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbCommonData wp = new DbCommonData();

                wp.id = dataId;
                wp.isDel = 1;
                wp.delDate = DateTime.Now.ToString();
                wp.delUser = userLogin.UserInfo().userCode;

                oper = new OperateModel();
                oper.Data = wp;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";

                rsl = cmd.Edit(oper);

            }
            catch (Exception e)
            {
                rsl.Message = e.Message;
            }

            return rsl;
        }


        private DataTable createTempTable()
        {
           var tempTable = new DataTable();
            tempTable.Columns.Add("gwCode");
            tempTable.Columns.Add("id");
            tempTable.Columns.Add("gwName");
            tempTable.Columns.Add("pCount");
            tempTable.Columns.Add("zhanbi");
            return tempTable;
        }
        /// <summary>
        /// 岗位信息添加
        /// </summary>
        /// <param name="tempCode">临时码</param>
        /// <param name="datas">数据</param>
        /// <returns></returns>
        public ReturnResult AddProductPosition(string tempCode, string datas, string gwName)
        {
            rsl = new ReturnResult();
            try
            {
                var tempTable =(DataTable)Tools.Cache.GetCache(tempCode);
                var json = JsonConvert.DeserializeObject<JObject>(datas);
                if (tempTable == null )
                {
                    tempTable = createTempTable();
                }
                bool isAdd = true;
                if (tempTable.Rows.Count >0)
                {
                    #region 修改岗位人数
                    for (int i = 0; i < tempTable.Rows.Count; i++)
                    {
                        if (tempTable.Rows[i]["gwCode"].ToString() == json["gangwei"].ToString())
                        {
                            isAdd = false;
                            if (tempTable.Rows[i]["zhanbi"].ToString() != json["zhanbi"].ToString())
                            {
                                rsl.Message = "岗位占比不同，禁止录入";
                                return rsl;
                            }
                            tempTable.Rows[i]["pCount"] =Convert.ToString(int.Parse(tempTable.Rows[i]["pCount"].ToString())+1);
                            break;
                        }
                    }
               
                    #endregion
                }
                if (isAdd)
                {
                    DataRow r = tempTable.NewRow();
                    r["gwCode"] = json["gangwei"].ToString();
                    r["gwName"] = gwName;
                    r["pCount"] = json["pCount"].ToString();
                    r["zhanbi"] = json["zhanbi"].ToString();
                    tempTable.Rows.Add(r);
                    rsl.Result = true;
                }
                Tools.Cache.SetCache(tempCode, tempTable);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 生产岗位信息保存
        /// </summary>
        /// <param name="tempCode"></param>
        /// <param name="gxCode"></param>
        /// <returns></returns>
        public ReturnResult SaveProductPosition(string tempCode,string gxCode)
        {
            rsl = new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                if (string.IsNullOrWhiteSpace(tempCode))
                {
                    rsl.Message = "关键参数丢失";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(gxCode))
                {
                    rsl.Message = "请选择工序";
                    return rsl;
                }
                var tempTable = (DataTable)Tools.Cache.GetCache(tempCode);
                if (tempTable == null || tempTable.Rows.Count < 1)
                {
                    rsl.Message = "没有要保存的数据";
                    return rsl;
                }
                StringBuilder sb = new StringBuilder();
       
                DbPositionInfo p;
                foreach (DataRow r in tempTable.Rows)
                {
                    if (string.IsNullOrWhiteSpace(r["id"].ToString()))
                    {
                        #region 添加数据
                        p = new DbPositionInfo();
                        p.createtime = getNow();
                        p.createuser = userLogin.UserInfo().userCode;
                        p.gwCode = r["gwCode"].ToString();
                        p.gxCode = gxCode;
                        p.isdel = 0;
                        p.persons = r["pCount"].ToString();
                        p.zhanbi = r["zhanbi"].ToString();
                        p.pCode = tempCode;
                        oper = new OperateModel();
                        oper.Data = p;
                        oper.Types = OperateModel.OperateType.Add;
                        operList.Add(oper);
                        #endregion
                    }

                }
                if (operList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(operList);
                }
                
           
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 获取生产岗位信息
        /// </summary>
        /// <returns></returns>
        public ReturnResult getPositonInfo()
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select p.* from fun_Position_info p ");
                sb.Append("where p.isdel=0");
                var table = cmd.GetDataTable(sb.ToString());
                sb.Clear();
                sb.Append("select cCode,cName from fun_CommonData_Info where isdel=0");
                sb.Append(" and cType in('scgw','jczd') and status='Y' ");
                var cTable = cmd.GetDataTable(sb.ToString());
                table.Columns.Add("gxName");
                for (int i = 1; i < 6; i++)
                {
                    table.Columns.Add("positionName" + i);

                }
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var dr = cTable.Select("cCode='" + table.Rows[i]["gxCode"].ToString() + "'");
                    if (dr.Length > 0)
                    {
                        table.Rows[i]["gxName"] = dr[0]["cName"].ToString();
                    }
                    for (int x = 1; x < 6; x++)
                    {
                        if (table.Rows[i]["position" + x] != null && !string.IsNullOrWhiteSpace(table.Rows[i]["position" + x].ToString()))
                        {
                            dr = cTable.Select("cCode='" + table.Rows[i]["position" + x].ToString() + "'");
                            if (dr.Length < 1)
                            {
                                table.Rows[i]["positionName" + x] = "未知";
                            }
                            else
                            {
                                table.Rows[i]["positionName" + x] = dr[0]["cName"].ToString();
                            }
                        }
                    }
                }
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
      /// <summary>
      /// 根据岗位编码获取生产岗位信息
      /// </summary>
      /// <param name="tempCode"></param>
      /// <returns></returns>
        public ReturnResult getPositonByCode(string tempCode)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select p.*,cm.cName gwName from fun_ProductPosition_info p ");
                sb.Append("inner join fun_CommonData_Info cm on p.gwCode=cm.cCode and cm.cType in('scgw','jczd')");
                sb.Append("where p.isdel=0");
                var table = cmd.GetDataTable(sb.ToString());
                if (table.Rows.Count > 0)
                {
                    var tempTable = createTempTable();
                    foreach (DataRow row in table.Rows)
                    {
                        DataRow r = tempTable.NewRow();
                        r["id"] = row["id"].ToString();
                        r["gwCode"] = row["gwCode"].ToString();
                        r["gwName"] = row["gwName"].ToString();
                        r["pCount"] = row["persons"].ToString();
                        r["zhanbi"] = row["zhanbi"].ToString();
                        tempTable.Rows.Add(r);
                    }
                    Tools.Cache.SetCache(table.Rows[0]["pCode"].ToString(), tempTable);
                    rsl.Data = cmd.TableToDictionary(tempTable);
                    rsl.Result = true;
                }

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 获取生产信息界面
        /// </summary>
        /// <param name="pCode"></param>
        /// <returns></returns>
        public ReturnResult getPositionInfo(string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from V_ProductPosition_info where isdel=0");
                sb = CreateSelectCondition(sb,condition);
                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 删除生产岗位
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelPositionInfo(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbPositionInfo db = new DbPositionInfo();
                db = new DbPositionInfo();
                db.isdel = 1;
                db.delDate = getNow();
                db.delUser = userLogin.UserInfo().userCode;
                db.id = int.Parse(dataId);
                oper = new OperateModel();
                oper.Data = db;
                oper.Conditions = "id";
                oper.Types = OperateModel.OperateType.Edit;
                rsl = cmd.Edit(oper);
           
               
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 删除岗位数据
        /// </summary>
        /// <param name="pCode"></param>
        /// <param name="gwCode"></param>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelPositionByCode(string pCode,string gwCode,string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                DbPositionInfo db = new DbPositionInfo();
                if (!string.IsNullOrWhiteSpace(dataId))
                {
                    db = new DbPositionInfo();
                    db.isdel = 1;
                    db.delDate = getNow();
                    db.delUser = userLogin.UserInfo().userCode;
                    db.id = int.Parse(dataId);
                    oper = new OperateModel();
                    oper.Data = db;
                    oper.Conditions = "id";
                    oper.Types = OperateModel.OperateType.Edit;
                    rsl = cmd.Edit(oper);
                }
                if (rsl.Result)
                {
                    var table = (DataTable)Tools.Cache.GetCache(pCode);
                    Tools.Cache.SetCache(pCode, table);
                    foreach (DataRow r in table.Rows)
                    {
                        if (r["gwCode"].ToString() == gwCode)
                        {
                            r.BeginEdit();
                            r.Delete();
                            r.EndEdit();
                        }
                    }
                }
               
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 设置人员生产职务
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public ReturnResult SetUserPosition(string datas)
        {
            rsl = new ReturnResult();
            try
            {
                var json = JsonConvert.DeserializeObject<JObject>(datas);
                var users = json["svalue"].ToString().Split(',');
                operList = new List<OperateModel>();
                if (string.IsNullOrWhiteSpace(json["gangwei"].ToString()))
                {
                    rsl.Message = "岗位信息不允许为空";
                    return rsl;
                }
                DbUser user;
                foreach (string u in users)
                {
                    if (!string.IsNullOrWhiteSpace(u))
                    {
                        user = new DbUser();
                        user.userCode = u;
                        user.zhiwu = json["gangwei"].ToString();
                        oper = new OperateModel();
                        oper.Data = user;
                        oper.Types = OperateModel.OperateType.Edit;
                        oper.Conditions = "userCode";
                        operList.Add(oper);
                    }

                }
                rsl = cmd.MoreUpdate(operList);

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 收发类型
        /// </summary>
        /// <param name="isAuto"></param>
        /// <returns></returns>
        public ReturnResult ShouFaInfo(bool isAuto = false)
        {
            rsl = new ReturnResult();
            SysFrameWork.SystemBLL sysBLL = new SysFrameWork.SystemBLL();
            CuDbHelper cuHelper = new CuDbHelper();
            try
            {
                StringBuilder sb = new StringBuilder();

                string baseCode = "";
                #region 账套选择
                if (isAuto)
                {
                    baseCode = sysBLL.getDefaultBaseCode();
                }
                else
                {
                    baseCode = userLogin.UserInfo().baseCode;
                }
                #endregion


                sb.Append("select cRdCode AS code, cRdName AS name,bRdFlag from Rd_Style");
                sb.Append(" where bRdEnd = 1  ");
                var table = cuHelper.getData(sb.ToString(), baseCode);

                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 人员岗位数据保存
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public ReturnResult SaveUserPosition(string datas)
        {
            rsl = new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                var json = JsonConvert.DeserializeObject<JObject>(datas);
                if (string.IsNullOrWhiteSpace(json["usersCode"].ToString()))
                {
                    rsl.Message = "请先选择人员";
                    return rsl;
                }
                if (string.IsNullOrWhiteSpace(json["gxCode"].ToString()))
                {
                    rsl.Message = "请选择工序";
                    return rsl;
                }
                var arr = json["usersCode"].ToString().Split(',');
                DbUserPosition up;
                foreach (var u in arr)
                {
                    if (!string.IsNullOrWhiteSpace(u))
                    {
                        up = new DbUserPosition();
                        up.createtime = getNow();
                        up.userCode =u;
                        up.createuser = userLogin.UserInfo().userCode;
                        up.gangwei = json["gangwei"].ToString();
                        up.gxCode = json["gxCode"].ToString();
                        up.machine = json["machine"].ToString();
                        up.zhanbi = json["zhanbi"].ToString();

                        up.isdel = 0;
                        oper = new OperateModel();
                        oper.Data = up;
                        oper.Types = OperateModel.OperateType.Add;
                        operList.Add(oper);
                    }

                }

                if (operList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(operList);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 人员岗位信息删除
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelUserPosition(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                DbUserPosition up = new DbUserPosition();
                up.id = int.Parse(dataId);
                up.isdel = 1;
                up.delDate = getNow();
                up.deluser = userLogin.UserInfo().userCode;
                oper = new OperateModel();
                oper.Data = up;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "id";
                rsl = cmd.Edit(oper);

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 工序条码类型
        /// </summary>
        /// <returns></returns>
        public ReturnResult barTypes()
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from fun_CommonData_Info ");
                sb.Append("where ctype in('bart','progx')");
                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 加载计划状态
        /// </summary>
        /// <param name="sCodes"></param>
        /// <returns></returns>
        public ReturnResult loadPlanStatus(string sCodes)
        {
            rsl = new ReturnResult();
            try
            {
                var arr = sCodes.Split(',');
                string status = "";
                foreach (string s in arr)
                {
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        status += "'" + s + "',";
                    }
                }
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from sys_dictionaries_info where code in");
                sb.Append("("+status.TrimEnd(',')+")");
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 获取用户关联工序
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ReturnResult getUserGongXu(string userCode)
        {
            rsl = new ReturnResult();
            try
            {
                //var json = JsonConvert.DeserializeObject<JObject>(conditions);
                StringBuilder sb = new StringBuilder();
                sb.Append("select cCode gxCode,cName gxName,orderNum,'0' cgx,'' gxPrice ");
                sb.Append(" from fun_CommonData_Info where isdel=0 and status='Y' and cType='Gx'");
                sb.Append(" and cCode is not null and cCode !=''");
                var gxTable = cmd.GetDataTable(sb.ToString());

                if (!string.IsNullOrWhiteSpace(userCode))
                {
                    sb.Clear();
                    sb.Append("select * from pi_GongXuUser_set ");
                    sb.Append("where usCode='" + userCode + "'");
                    var uGxTable = cmd.GetDataTable(sb.ToString());
                    for (int i = 0; i < gxTable.Rows.Count; i++)
                    {
                        string gxCode = gxTable.Rows[i]["gxCode"].ToString();
                        var dr = uGxTable.Select("GongXu='" + gxCode + "'");
                        if (dr.Length > 0)
                        {
                            gxTable.Rows[i]["cgx"] = "1";
                            //gxTable.Rows[i]["orderNum"] = dr[0]["gxOrder"].ToString();
                        }

                    }
                }
                rsl.Data = cmd.TableToDictionary(gxTable);
                rsl.Result = true;
                //StringBuilder sb = new StringBuilder();
                //sb.Append("select * from pi_GongXuUser_set where ");
                //sb.Append("usCode='"+ userCode +"'");
                //var table = cmd.GetDataTable(sb.ToString());
                //string gxCodes = "";
                //foreach (DataRow r in table.Rows)
                //{
                //    gxCodes += r["GongXu"].ToString()+",";
                //}
                //if (!string.IsNullOrWhiteSpace(gxCodes))
                //{
                //    rsl.Data = gxCodes.TrimEnd(',');
                //    rsl.Result = true;
                //}
             
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 保存人员工序数据
        /// </summary>
        /// <param name="userCode"></param>
        /// <param name="gxCodes"></param>
        /// <returns></returns>
        public ReturnResult SaveUserGongXu(string userCode, string gxStr)
        {
            ReturnResult rsl = new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                DbUserGX user;
                string sql = "select * from pi_GongXuUser_set";
                var userTable = cmd.GetDataTable(sql);
                #region 添加
                if (!string.IsNullOrWhiteSpace(userCode))
                {
                    var gxArr = gxStr.Split(',');
                    foreach (string gStr in gxArr)
                    {
                        #region
                        var gArr = gStr.Split('-');
                        var dr = userTable.Select("usCode='" + userCode + "' and GongXu='" + gArr[0]+"'");
                        user = new DbUserGX();
                        oper = new OperateModel();
                        user.GongXu = gArr[0];
                        user.usCode = userCode;
                        user.createtime = getNow();
                        user.createuser = userLogin.UserInfo().userCode;
                        if (dr.Length > 0 && dr[0]["GongXu"].ToString() == gArr[0])
                        {
                            oper.Types = OperateModel.OperateType.Edit;
                            oper.Conditions = "usCode,GongXu";
                        }
                        else
                        {
                            oper.Types = OperateModel.OperateType.Add;
                        }
                        oper.Data = user;
                        operList.Add(oper);
                        #endregion
                    }
                    foreach (DataRow r in userTable.Rows)
                    {
                        string uCode = r["usCode"].ToString();

                        if (userCode == uCode) 
                        {
                            string gxCode = r["GongXu"].ToString();
                            bool isDel = true;
                            foreach (string gStr in gxArr)
                            {
                                var gArr = gStr.Split('-');
                                if (gArr[0] == gxCode)
                                {
                                    isDel = false;
                                    break;
                                }
                            }
                            if (isDel)
                            {
                                user = new DbUserGX();
                                user.id = int.Parse(r["id"].ToString());
                                oper = new OperateModel();
                                oper.Data = user;
                                oper.Types = OperateModel.OperateType.Delete;
                                operList.Add(oper);
                            }
                        }
                    }
                }

                #endregion
                if (operList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(operList);
                }

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 获取存货颜色信息
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public ReturnResult getInvproList(string condition,string dataType)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select cName from fun_CommonData_Info where specFile='" + userLogin.UserInfo().baseCode + "' ");
                sb.Append(" and isDel=0  ");//and status='Y'
                if (dataType.Equals("selcol"))
                    sb.Append(" and cType='proColor' ");//选择颜色
                else
                    sb.Append(" and cType='proBrand' ");//选择商标

                string nsb = "";
                nsb = sb.ToString()+" and parentCode is null ";   //不包含存货检索信息的

                sb = CreateSelectCondition(sb, condition);
                var table = cmd.GetDataTable(sb.ToString());
                if (table.Rows.Count < 1)
                {
                    var json = JsonConvert.DeserializeObject<JObject>(condition);
                    if (!string.IsNullOrWhiteSpace(json["cName"].ToString()))
                        nsb += " and cName like '%"+ json["cName"].ToString()+"%' ";
                    //如果存货没有设置自由项，则可以选择全部的自由项选项
                    table = cmd.GetDataTable(nsb);
                }

                var list = cmd.TableToDictionary(table);
                rsl.Data = list;
                rsl.Result = true;

            }
            catch (Exception ex)
            {
                rsl.Message = ex.ToString();
            }
            return rsl;

        }

        ///// <summary>
        ///// 获取仓库和货位信息
        ///// </summary>
        ///// <param name="jsonStr"></param>
        ///// <returns></returns>
        //public ReturnResult getWarePositions(string pageIndex, string pageSize, ref string pageCount, string condition)
        //{
        //    rsl = new ReturnResult();
        //    try
        //    {
        //        StringBuilder sb = new StringBuilder();
        //        sb.Append(" baseCode='" + userLogin.UserInfo().baseCode + "'");
        //        sb = CreateSelectCondition(sb, condition);
        //        var list = cmd.PageList("V_warePosition_info", pageIndex, pageSize, sb.ToString(), "wareCode", ref pageCount);
        //        rsl.Data = list;
        //        rsl.Result = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        rsl.Message = ex.ToString();
        //    }
        //    return rsl;

        //}

    }
}
